package com.iplant.mes.serviceImpl.dao.exc;

import com.iplant.ams.service.po.dms.DMSDeviceLedger;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.cache.CacheUtils;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.basicenum.bpm.BPMTaskStatus;
import com.iplant.basic.service.basicenum.stat.StatTypes;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.fmc.FMCStation;
import com.iplant.basic.serviceImpl.dao.TaskBaseDao;
import com.iplant.mes.service.po.exc.EXCStatRate;
import com.iplant.mes.service.po.exc.EXCUnusualTask;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@DBBaseDAO(EXCUnusualTask.class)
public class EXCUnusualTaskDAO extends TaskBaseDao<EXCUnusualTask> {

    private static EXCUnusualTaskDAO Instance = null;

    public static EXCUnusualTaskDAO getInstance() {
        if (Instance == null) {
            Instance = new EXCUnusualTaskDAO();
        }
        return Instance;
    }

    public EXCUnusualTaskDAO() {
        super(EXCUnusualTask.class);
        this.Init();
    }


    @Override
    protected void Initialize() {
        super.SetInstance(this);
    }


    /**
     * 类型趋势统计
     *
     * @param wLoginUser
     * @param wUnusualType
     * @param wDeviceID
     * @param wStationID
     * @param wStatType
     * @param wStartDate
     * @param wEndDate
     * @param wErrorCode
     * @return
     */
    public List<EXCStatRate> EXC_GetTypeRateList(BMSEmployee wLoginUser, int wUnusualType, int wDeviceID,
                                                 int wStationID, int wStatType,
                                                 Calendar wStartDate, Calendar wEndDate,
                                                 OutResult<Integer> wErrorCode) {
        List<EXCStatRate> wResult = new ArrayList<>();
        try {
            StatTypes wType = StatTypes.getEnumType(wStatType);
            String wGroupType = "";
            switch (wType) {
                case Week:
                    wGroupType = " str_to_date( DATE_FORMAT(t.CreateTime,'%Y-%u-1'),'%Y-%u-%w') ";

                    break;
                case Month:
                    wGroupType = " str_to_date( DATE_FORMAT(t.CreateTime,'%Y-%m-1'),'%Y-%m-%d') ";

                    break;
                case Quarter:
                    wGroupType = " str_to_date( concat(  year(t.CreateTime),'-',(quarter(t.CreateTime) * 3)-2,'-1')," +
                            "'%Y-%m-%d') ";

                    break;
                case Year:
                    wGroupType = " str_to_date( concat(  year(t.CreateTime),'-01','-01'),'%Y-%m-%d')  ";

                    break;
                default:
                    wGroupType = " str_to_date( DATE_FORMAT(t.CreateTime,'%Y-%m-%d'),'%Y-%m-%d') ";
                    break;
            }

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("wFinishStatus", BPMTaskStatus.Confirm.getValue());
            wParams.put("wRejectStatus", BPMTaskStatus.Rejected.getValue());
            wParams.put("wUnusualType", wUnusualType);
            wParams.put("wDeviceID", wDeviceID);
            wParams.put("wStationID", wStationID);
            wParams.put("wStartDate", wStartDate);
            wParams.put("wEndDate", wEndDate);


            String wSQLText = StringUtils.Format("SELECT t.UnusualType,  {1}  as StatTypeDate," +
                    "  count(case when t.Status=:wFinishStatus  then t.ID else null end) as Times," +
                    "  count(case when t.Status=:wRejectStatus  then t.ID else null end) as RejectTimes," +
                    "  count(case when t.Status > 0  and t.Status< :wFinishStatus then t.ID else null end) as " +
                    "CurrentTimes " +
                    "  FROM {0} t where ID>0 and (:wUnusualType<=0 or t.UnusualType=:wUnusualType) " +
                    "  and (:wDeviceID<=0 or t.DeviceID=:wDeviceID)" +
                    "  and (:wStationID<=0 or t.StationID=:wStationID)" +
                    "  and (:wStartDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wStartDate <= t.CreateTime)" +
                    " " +
                    "  and (:wEndDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') or :wEndDate >= t.CreateTime)  " +
                    "  GROUP BY t.UnusualType, StatTypeDate", getTableName(), wGroupType);

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQLText, wParams);

            if (wQueryResult == null || wQueryResult.size() == 0)
                return wResult;


            EXCStatRate wEXCStatRate;
            for (Map<String, Object> wReader : wQueryResult) {
                wEXCStatRate = new EXCStatRate();
                wEXCStatRate.UnusualType = StringUtils.parseInt(wReader.get("UnusualType"));
                wEXCStatRate.StatDate = StringUtils.parseCalendar(wReader.get("StatTypeDate"));
                wEXCStatRate.Times = StringUtils.parseInt(wReader.get("Times"));
                wEXCStatRate.RejectTimes = StringUtils.parseInt(wReader.get("RejectTimes"));
                wEXCStatRate.CurrentTimes = StringUtils.parseInt(wReader.get("CurrentTimes"));

                wResult.add(wEXCStatRate);
            }

        } catch (Exception ex) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            log.error("EXC_GetTypeRateList", ex);
        }
        return wResult;
    }

    /**
     * 工位趋势统计
     *
     * @param wLoginUser
     * @param wUnusualType
     * @param wDeviceID
     * @param wStationID
     * @param wStatType
     * @param wStartDate
     * @param wEndDate
     * @param wErrorCode
     * @return
     */
    public List<EXCStatRate> EXC_GetStationRateList(BMSEmployee wLoginUser, int wUnusualType, int wDeviceID,
                                                    int wStationID, int wStatType,
                                                    Calendar wStartDate, Calendar wEndDate,
                                                    OutResult<Integer> wErrorCode) {
        List<EXCStatRate> wResult = new ArrayList<>();
        try {
            StatTypes wType = StatTypes.getEnumType(wStatType);
            String wGroupType = "";
            switch (wType) {
                case Week:
                    wGroupType = " str_to_date( DATE_FORMAT(t.CreateTime,'%Y-%u-1'),'%Y-%u-%w') ";

                    break;
                case Month:
                    wGroupType = " str_to_date( DATE_FORMAT(t.CreateTime,'%Y-%m-1'),'%Y-%m-%d') ";

                    break;
                case Quarter:
                    wGroupType = " str_to_date( concat(  year(t.CreateTime),'-',(quarter(t.CreateTime) * 3)-2,'-1')," +
                            "'%Y-%m-%d') ";

                    break;
                case Year:
                    wGroupType = " str_to_date( concat(  year(t.CreateTime),'-01','-01'),'%Y-%m-%d')  ";

                    break;
                default:
                    wGroupType = " str_to_date( DATE_FORMAT(t.CreateTime,'%Y-%m-%d'),'%Y-%m-%d') ";
                    break;
            }

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("wFinishStatus", BPMTaskStatus.Confirm.getValue());
            wParams.put("wRejectStatus", BPMTaskStatus.Rejected.getValue());
            wParams.put("wUnusualType", wUnusualType);
            wParams.put("wDeviceID", wDeviceID);
            wParams.put("wStationID", wStationID);
            wParams.put("wStartDate", wStartDate);
            wParams.put("wEndDate", wEndDate);


            String wSQLText = StringUtils.Format("SELECT t.StationID,  {1}  as StatTypeDate," +
                    "  count(case when t.Status=:wFinishStatus  then t.ID else null end) as Times," +
                    "  count(case when t.Status=:wRejectStatus  then t.ID else null end) as RejectTimes," +
                    "  count(case when t.Status > 0  and t.Status< :wFinishStatus then t.ID else null end) as " +
                    "CurrentTimes " +
                    "  FROM {0} t where ID>0 AND t.StationID>0  and (:wUnusualType<=0 or t.UnusualType=:wUnusualType)" +
                    " " +
                    "  and (:wDeviceID<=0 or t.DeviceID=:wDeviceID)" +
                    "  and (:wStationID<=0 or t.StationID=:wStationID)" +
                    "  and (:wStartDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wStartDate <= t.CreateTime)" +
                    " " +
                    "  and (:wEndDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') or :wEndDate >= t.CreateTime)  " +
                    "  GROUP BY t.StationID, StatTypeDate", getTableName(), wGroupType);

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQLText, wParams);
            if (wQueryResult == null || wQueryResult.size() == 0)
                return wResult;

            Map<Integer, FMCStation> wFMCStationMap = CacheUtils.GetCacheTList(FMCStation.class).stream()
                    .collect(Collectors.toMap(p -> p.ID, p -> p));
            EXCStatRate wEXCStatRate;
            for (Map<String, Object> wReader : wQueryResult) {
                wEXCStatRate = new EXCStatRate();
                wEXCStatRate.StationID = StringUtils.parseInt(wReader.get("StationID"));
                wEXCStatRate.StatDate = StringUtils.parseCalendar(wReader.get("StatTypeDate"));
                wEXCStatRate.Times = StringUtils.parseInt(wReader.get("Times"));
                wEXCStatRate.RejectTimes = StringUtils.parseInt(wReader.get("RejectTimes"));
                wEXCStatRate.CurrentTimes = StringUtils.parseInt(wReader.get("CurrentTimes"));

                if (wFMCStationMap.containsKey(wEXCStatRate.StationID)) {
                    wEXCStatRate.StationName = wFMCStationMap.get(wEXCStatRate.StationID).Name;
                    wEXCStatRate.StationCode = wFMCStationMap.get(wEXCStatRate.StationID).Code;
                }
                wResult.add(wEXCStatRate);
            }
        } catch (Exception ex) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            log.error("EXC_GetStationRateList", ex);
        }
        return wResult;
    }

    /**
     * 设备趋势统计
     *
     * @param wLoginUser
     * @param wUnusualType
     * @param wDeviceID
     * @param wStationID
     * @param wStatType
     * @param wStartDate
     * @param wEndDate
     * @param wErrorCode
     * @return
     */
    public List<EXCStatRate> EXC_GetDeviceRateList(BMSEmployee wLoginUser, int wUnusualType, int wDeviceID,
                                                   int wStationID, int wStatType,
                                                   Calendar wStartDate, Calendar wEndDate,
                                                   OutResult<Integer> wErrorCode) {
        List<EXCStatRate> wResult = new ArrayList<>();
        try {
            StatTypes wType = StatTypes.getEnumType(wStatType);
            String wGroupType = "";
            switch (wType) {
                case Week:
                    wGroupType = " str_to_date( DATE_FORMAT(t.CreateTime,'%Y-%u-1'),'%Y-%u-%w') ";

                    break;
                case Month:
                    wGroupType = " str_to_date( DATE_FORMAT(t.CreateTime,'%Y-%m-1'),'%Y-%m-%d') ";

                    break;
                case Quarter:
                    wGroupType = " str_to_date( concat(  year(t.CreateTime),'-',(quarter(t.CreateTime) * 3)-2,'-1')," +
                            "'%Y-%m-%d') ";

                    break;
                case Year:
                    wGroupType = " str_to_date( concat(  year(t.CreateTime),'-01','-01'),'%Y-%m-%d')  ";

                    break;
                default:
                    wGroupType = " str_to_date( DATE_FORMAT(t.CreateTime,'%Y-%m-%d'),'%Y-%m-%d') ";
                    break;
            }

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("wFinishStatus", BPMTaskStatus.Confirm.getValue());
            wParams.put("wRejectStatus", BPMTaskStatus.Rejected.getValue());
            wParams.put("wUnusualType", wUnusualType);
            wParams.put("wDeviceID", wDeviceID);
            wParams.put("wStationID", wStationID);
            wParams.put("wStartDate", wStartDate);
            wParams.put("wEndDate", wEndDate);


            String wSQLText = StringUtils.Format("SELECT t.DeviceID,  {1}  as StatTypeDate," +
                    "  count(case when t.Status=:wFinishStatus  then t.ID else null end) as Times," +
                    "  count(case when t.Status=:wRejectStatus  then t.ID else null end) as RejectTimes," +
                    "  count(case when t.Status > 0  and t.Status< :wFinishStatus then t.ID else null end) as " +
                    "CurrentTimes " +
                    "  FROM {0} t where ID>0 AND t.DeviceID>0  and (:wUnusualType<=0 or t.UnusualType=:wUnusualType) " +
                    "  and (:wDeviceID<=0 or t.DeviceID=:wDeviceID)" +
                    "  and (:wStationID<=0 or t.StationID=:wStationID)" +
                    "  and (:wStartDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wStartDate <= t.CreateTime)" +
                    " " +
                    "  and (:wEndDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') or :wEndDate >= t.CreateTime)  " +
                    "  GROUP BY t.DeviceID, StatTypeDate", getTableName(), wGroupType);

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQLText, wParams);
            if (wQueryResult == null || wQueryResult.size() == 0)
                return wResult;

            Map<Integer, DMSDeviceLedger> wDMSDeviceLedgerMap = CacheUtils.GetCacheTList(DMSDeviceLedger.class).stream()
                    .collect(Collectors.toMap(p -> p.ID, p -> p));
            EXCStatRate wEXCStatRate;
            for (Map<String, Object> wReader : wQueryResult) {
                wEXCStatRate = new EXCStatRate();
                wEXCStatRate.DeviceID = StringUtils.parseInt(wReader.get("DeviceID"));
                wEXCStatRate.StatDate = StringUtils.parseCalendar(wReader.get("StatTypeDate"));
                wEXCStatRate.Times = StringUtils.parseInt(wReader.get("Times"));
                wEXCStatRate.RejectTimes = StringUtils.parseInt(wReader.get("RejectTimes"));
                wEXCStatRate.CurrentTimes = StringUtils.parseInt(wReader.get("CurrentTimes"));

                if (wDMSDeviceLedgerMap.containsKey(wEXCStatRate.DeviceID)) {
                    wEXCStatRate.DeviceName = wDMSDeviceLedgerMap.get(wEXCStatRate.DeviceID).Name;
                    wEXCStatRate.DeviceCode = wDMSDeviceLedgerMap.get(wEXCStatRate.DeviceID).Code;
                    wEXCStatRate.AssetInfoID = wDMSDeviceLedgerMap.get(wEXCStatRate.DeviceID).AssetInfoID;
                }
                wResult.add(wEXCStatRate);
            }
        } catch (Exception ex) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            log.error("EXC_GetDeviceRateList", ex);
        }
        return wResult;
    }


    /**
     * 类型趋势统计
     *
     * @param wLoginUser
     * @param wUnusualType
     * @param wDeviceID
     * @param wStationID
     * @param wStartDate
     * @param wEndDate
     * @param wErrorCode
     * @return
     */
    public List<EXCStatRate> EXC_GetTypeRateList(BMSEmployee wLoginUser, int wUnusualType, int wDeviceID,
                                                 int wStationID,
                                                 Calendar wStartDate, Calendar wEndDate,
                                                 OutResult<Integer> wErrorCode) {
        List<EXCStatRate> wResult = new ArrayList<>();
        try {


            Map<String, Object> wParams = new HashMap<>();
            wParams.put("wFinishStatus", BPMTaskStatus.Confirm.getValue());
            wParams.put("wRejectStatus", BPMTaskStatus.Rejected.getValue());
            wParams.put("wUnusualType", wUnusualType);
            wParams.put("wDeviceID", wDeviceID);
            wParams.put("wStationID", wStationID);
            wParams.put("wStartDate", wStartDate);
            wParams.put("wEndDate", wEndDate);


            String wSQLText = StringUtils.Format("SELECT t.UnusualType, " +
                    "  count(case when t.Status=:wFinishStatus  then t.ID else null end) as Times," +
                    "  count(case when t.Status=:wRejectStatus  then t.ID else null end) as RejectTimes," +
                    "  count(case when t.Status > 0  and t.Status< :wFinishStatus then t.ID else null end) as " +
                    "CurrentTimes " +
                    "  FROM {0} t where ID>0 and (:wUnusualType<=0 or t.UnusualType=:wUnusualType) " +
                    "  and (:wDeviceID<=0 or t.DeviceID=:wDeviceID)" +
                    "  and (:wStationID<=0 or t.StationID=:wStationID)" +
                    "  and (:wStartDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wStartDate <= t.CreateTime)" +
                    " " +
                    "  and (:wEndDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') or :wEndDate >= t.CreateTime)  " +
                    "  GROUP BY t.UnusualType", getTableName());

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQLText, wParams);

            if (wQueryResult == null || wQueryResult.size() == 0)
                return wResult;

            EXCStatRate wEXCStatRate;
            for (Map<String, Object> wReader : wQueryResult) {
                wEXCStatRate = new EXCStatRate();
                wEXCStatRate.UnusualType = StringUtils.parseInt(wReader.get("UnusualType"));
                wEXCStatRate.Times = StringUtils.parseInt(wReader.get("Times"));
                wEXCStatRate.RejectTimes = StringUtils.parseInt(wReader.get("RejectTimes"));
                wEXCStatRate.CurrentTimes = StringUtils.parseInt(wReader.get("CurrentTimes"));

                wResult.add(wEXCStatRate);
            }

        } catch (Exception ex) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            log.error("EXC_GetTypeRateList", ex);
        }
        return wResult;
    }

    /**
     * 工位趋势统计
     *
     * @param wLoginUser
     * @param wUnusualType
     * @param wDeviceID
     * @param wStationID
     * @param wStartDate
     * @param wEndDate
     * @param wErrorCode
     * @return
     */
    public List<EXCStatRate> EXC_GetStationRateList(BMSEmployee wLoginUser, int wUnusualType, int wDeviceID,
                                                    int wStationID,
                                                    Calendar wStartDate, Calendar wEndDate,
                                                    OutResult<Integer> wErrorCode) {
        List<EXCStatRate> wResult = new ArrayList<>();
        try {

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("wFinishStatus", BPMTaskStatus.Confirm.getValue());
            wParams.put("wRejectStatus", BPMTaskStatus.Rejected.getValue());
            wParams.put("wUnusualType", wUnusualType);
            wParams.put("wDeviceID", wDeviceID);
            wParams.put("wStationID", wStationID);
            wParams.put("wStartDate", wStartDate);
            wParams.put("wEndDate", wEndDate);


            String wSQLText = StringUtils.Format("SELECT t.StationID," +
                    "  count(case when t.Status=:wFinishStatus  then t.ID else null end) as Times," +
                    "  count(case when t.Status=:wRejectStatus  then t.ID else null end) as RejectTimes," +
                    "  count(case when t.Status > 0  and t.Status< :wFinishStatus then t.ID else null end) as " +
                    "CurrentTimes " +
                    "  FROM {0} t where ID>0 AND t.StationID>0  and (:wUnusualType<=0 or t.UnusualType=:wUnusualType)" +
                    " " +
                    "  and (:wDeviceID<=0 or t.DeviceID=:wDeviceID)" +
                    "  and (:wStationID<=0 or t.StationID=:wStationID)" +
                    "  and (:wStartDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wStartDate <= t.CreateTime)" +
                    " " +
                    "  and (:wEndDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') or :wEndDate >= t.CreateTime)  " +
                    "  GROUP BY t.StationID", getTableName());

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQLText, wParams);
            if (wQueryResult == null || wQueryResult.size() == 0)
                return wResult;

            Map<Integer, FMCStation> wFMCStationMap = CacheUtils.GetCacheTList(FMCStation.class).stream()
                    .collect(Collectors.toMap(p -> p.ID, p -> p));
            EXCStatRate wEXCStatRate;
            for (Map<String, Object> wReader : wQueryResult) {
                wEXCStatRate = new EXCStatRate();
                wEXCStatRate.StationID = StringUtils.parseInt(wReader.get("StationID"));
                wEXCStatRate.Times = StringUtils.parseInt(wReader.get("Times"));
                wEXCStatRate.RejectTimes = StringUtils.parseInt(wReader.get("RejectTimes"));
                wEXCStatRate.CurrentTimes = StringUtils.parseInt(wReader.get("CurrentTimes"));

                if (wFMCStationMap.containsKey(wEXCStatRate.StationID)) {
                    wEXCStatRate.StationName = wFMCStationMap.get(wEXCStatRate.StationID).Name;
                    wEXCStatRate.StationCode = wFMCStationMap.get(wEXCStatRate.StationID).Code;
                }
                wResult.add(wEXCStatRate);
            }
        } catch (Exception ex) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            log.error("EXC_GetStationRateList", ex);
        }
        return wResult;
    }

    /**
     * 设备趋势统计
     *
     * @param wLoginUser
     * @param wUnusualType
     * @param wDeviceID
     * @param wStationID
     * @param wStartDate
     * @param wEndDate
     * @param wErrorCode
     * @return
     */
    public List<EXCStatRate> EXC_GetDeviceRateList(BMSEmployee wLoginUser, int wUnusualType, int wDeviceID,
                                                   int wStationID,
                                                   Calendar wStartDate, Calendar wEndDate,
                                                   OutResult<Integer> wErrorCode) {
        List<EXCStatRate> wResult = new ArrayList<>();
        try {

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("wFinishStatus", BPMTaskStatus.Confirm.getValue());
            wParams.put("wRejectStatus", BPMTaskStatus.Rejected.getValue());
            wParams.put("wUnusualType", wUnusualType);
            wParams.put("wDeviceID", wDeviceID);
            wParams.put("wStationID", wStationID);
            wParams.put("wStartDate", wStartDate);
            wParams.put("wEndDate", wEndDate);


            String wSQLText = StringUtils.Format("SELECT t.DeviceID, " +
                    "  count(case when t.Status=:wFinishStatus  then t.ID else null end) as Times," +
                    "  count(case when t.Status=:wRejectStatus  then t.ID else null end) as RejectTimes," +
                    "  count(case when t.Status > 0  and t.Status< :wFinishStatus then t.ID else null end) as " +
                    "CurrentTimes " +
                    "  FROM {0} t where ID>0 AND t.DeviceID>0  and (:wUnusualType<=0 or t.UnusualType=:wUnusualType) " +
                    "  and (:wDeviceID<=0 or t.DeviceID=:wDeviceID)" +
                    "  and (:wStationID<=0 or t.StationID=:wStationID)" +
                    "  and (:wStartDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wStartDate <= t.CreateTime)" +
                    " " +
                    "  and (:wEndDate <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') or :wEndDate >= t.CreateTime)  " +
                    "  GROUP BY t.DeviceID", getTableName());

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQLText, wParams);
            if (wQueryResult == null || wQueryResult.size() == 0)
                return wResult;

            Map<Integer, DMSDeviceLedger> wDMSDeviceLedgerMap = CacheUtils.GetCacheTList(DMSDeviceLedger.class).stream()
                    .collect(Collectors.toMap(p -> p.ID, p -> p));
            EXCStatRate wEXCStatRate;
            for (Map<String, Object> wReader : wQueryResult) {
                wEXCStatRate = new EXCStatRate();
                wEXCStatRate.DeviceID = StringUtils.parseInt(wReader.get("DeviceID"));
                wEXCStatRate.Times = StringUtils.parseInt(wReader.get("Times"));
                wEXCStatRate.RejectTimes = StringUtils.parseInt(wReader.get("RejectTimes"));
                wEXCStatRate.CurrentTimes = StringUtils.parseInt(wReader.get("CurrentTimes"));

                if (wDMSDeviceLedgerMap.containsKey(wEXCStatRate.DeviceID)) {
                    wEXCStatRate.DeviceName = wDMSDeviceLedgerMap.get(wEXCStatRate.DeviceID).Name;
                    wEXCStatRate.DeviceCode = wDMSDeviceLedgerMap.get(wEXCStatRate.DeviceID).Code;
                    wEXCStatRate.AssetInfoID = wDMSDeviceLedgerMap.get(wEXCStatRate.DeviceID).AssetInfoID;
                }
                wResult.add(wEXCStatRate);
            }
        } catch (Exception ex) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            log.error("EXC_GetDeviceRateList", ex);
        }
        return wResult;
    }

    /**
     * 更新创建时间
     */
    public void EXC_UpdateCreateTimeByID(BMSEmployee wLoginUser, int id, OutResult<Integer> wErrorCode) {
        try {
            String wSQL = StringUtils.Format("UPDATE {0}.`exc_unusual_task` SET CreateTime=NOW() WHERE ID=:ID;",
                    DBSchemas.MES.getDBName());

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wParamMap.put("ID", id);

            wSQL = this.DMLChange(wSQL);

            this.update(wSQL, wParamMap);
        } catch (Exception ex) {
            log.error(ex.toString());
        }
    }
}
